﻿
$(document).ready(
	function () {
		if ($('#preview').html().trim() != '') {
			var x = $('#preview').html();
			Transform(x);
		}
		else {
			$('#preview').html('<a href="user.aspx?uid=' + oDict.adminid + '">' + oDict.adminname + '</a>&nbsp;&nbsp;还未编辑完成，请期待吧');
		}
		if (oDict.isadmin) {
			$.each($('#admin_panel .action'), function () {
				var text = $(this).text().trim();
				if (text == '从其它词汇表导入解释') {
					$(this).bind("click", { fun: ImportParaphrase, title: TitleParaphrase }, ShowMiniPop);
				}
			});
		}

		$.each($('.action'), function () {
			var txt = $(this).text();

			if (txt == '删除') {
				var cid = $(this).parent().attr('data-id');
				$(this).bind("click", { fun: DeleteComment, title: "确认删除此评论？", argus: { cid: cid} }, ShowMiniPop);
			}
			else if (txt == '屏蔽') {
				var cid = $(this).parent().attr('data-id');
				$(this).bind("click", { fun: ScreenComment, title: "确认屏蔽此评论？", argus: { cid: cid} }, ShowMiniPop);
			}
			else if (txt == '回复') {
				$(this).bind("click", { did: oDict.did, uid: $(this).attr('data-uid'), name: $(this).attr('data-name') }, ReviewClick);
			}
			else if (txt == '提交') {
				$(this).bind("click",
				{
					fun: Comment,
					check: function () {
						if ($('#comment').val().trim() == '' || $('#comment').val() == '分享记忆点滴...')
							return '分享不能为空';
						else
							return '';
					},
					title: "",
					argus: { did: oDict.did }
				},
				ShowMiniHint);
			}
		});

		$.each($('#control_panel .action'), function () {
			var text = $(this).text().trim();
			if (text == '关注') {
				if (oDict.iscollect == true) {
					$(this).text('已关注');
					$(this).removeClass('follow').addClass('followed').attr('title', '点击取消关注');
				}
				$(this).bind("click", { fun: Follow, title: "", argus: { did: oDict.did} }, ShowMiniHint);
			}
		});

		$('#comment').bind("focus", function () {
			if ($(this).val() == '分享记忆点滴...') {
				$(this).val('');
			}
		});

		$('#edit').bind("click",
		{
			fun: Edit,
			check: function () {
				if ($('#paraphrase').val().trim() == '')
					return '解释不能为空';
				else
					return '';
			}
		},
		ShowMiniHint);

	}
);

function Edit() {
	$.ajax({
		url: '/Submit/submit.ashx?page=dict&did=' + oDict.did + '&action=020&rnd=' + Math.random(),
		type: 'POST',
		timeout: 5000,
		dataType: "json",
		data: { 'ph': $('#paraphrase').val(), 'ct': $('#commentate').val() },
		error: function () {
			AddMiniHint('操作失败，请重试');
		},
		success: function (json) {
			if (json.code == 0) {
				AddMiniHint('操作成功！');
				HideMiniPop(1000);
				$('#preview').html(Transform($('#paraphrase').val().replace(/\r\n/gim, '<br />').replace(/\n/gim, '<br />')));
			}
			else {
				AddMiniHint('操作失败，请重试');
			}
		}
	});
}

function ImportParaphrase() {
	var did = $("input[name='imports']:checked").val();

	$('#paraphrase').val(oDict.FDicts[did].ph);
	HideMiniPop();
}
function TitleParaphrase(fun) {
	$.ajax({
		url: '/Submit/submit.ashx?page=dict&action=019&did=' + oDict.did + '&rnd=' + Math.random(),
		type: 'GET',
		timeout: 5000,
		dataType: "json",
		error: function () {
			fun('操作失败，请重试', false);
		},
		success: function (json) {
			if (json.code >= 0) {
				var h = '<div id="fromdict"><h3>请选择来源单词</h3><ul>';
				oDict.FDicts = {};
				for (var i = 0; i < json.obj.length; i++) {
					var o = json.obj[i];
					oDict.FDicts[o.did] = o;
					h += '<li><div><input type="radio" name="imports" value="' + o.did + '">' + o.spell + '</div><div>' + GetPh(o.ph) + '</div></li>';
				}
				h += '<li><input type="radio" name="imports" value="0">其它，NO.<input style="width:60px;" type="text" value="">(<span>输入编码即可</span>)</li>';
				h += '</ul><div>';
				fun(h);
			}
			else {
				fun('操作失败，请重试', false);
			}
		}
	});
}

function GetPh(x) {
	x = BlodPh(x);
	x = x.replace(/\[en\](.*?)\[\/en\]/gim, '<div style="font-size:14px;">$1</div>')
	x = x.replace(/\[cn\](.*?)\[\/cn\]/gim, "<div>$1</div>");
	return x;
}


function Follow(argus) {
	if (userid == '') return;

	var did = argus.did;

	$.ajax({
		url: '/Submit/submit.ashx?page=list&action=014&did=' + did + '&rnd=' + Math.random(),
		type: 'GET',
		timeout: 5000,
		dataType: "json",
		error: function () {
			AddMiniHint('操作失败，请重试');
		},
		success: function (json) {
			if (json.code == 0) {
				AddMiniHint('操作成功！');
				var st = $('#control_panel > a').eq(1);
				if (st.text().trim() == '关注') {
					st.html('已关注');
					st.attr('title', '点击取消关注');
					st.removeClass('follow').addClass('followed');
				}
				else {
					st.html('关注');
					st.removeClass('followed').addClass('follow');
				}

				HideMiniPop(1000);
			}
			else {
				AddMiniHint('操作失败，请重试');
			}
		}
	});
}

function CheckLen(e) {
	var str = $(this).val();
	var len = str.replace(/[^\x00-\xff]/g, "**").length;
	$('#rlen').text(parseInt(e.data) - len)
}

function Reset() {
	$('#reset').click();
}

function DeleteComment(argus) {
	var cid = argus.data.cid;

	if (userid == '') return;
	$.ajax({
		url: '/Submit/submit.ashx?page=dict&action=001&cid=' + cid + '&rnd=' + Math.random(),
		type: 'GET',
		timeout: 5000,
		dataType: "json",
		error: function () {
			AddMiniHint('删除失败，请重试');
		},
		success: function (json) {
			if (json.code == 0) {
				$('#ct_' + cid).fadeOut(1000);
				$('#lbl_commentcount').html(parseInt($('#lbl_commentcount').html()) - 1);
				HideMiniPop();
			}
			else {
				AddMiniHint('删除失败，请重试');
			}
		}
	});
}

function ScreenComment(argus) {
	var cid = argus.data.cid;

	if (userid == '') return;
	$.ajax({
		url: '/Submit/submit.ashx?page=dict&action=002&cid=' + cid + '&rnd=' + Math.random(),
		type: 'GET',
		timeout: 5000,
		dataType: "json",
		error: function () {
			AddMiniHint('屏蔽失败，请重试');
		},
		success: function (json) {
			if (json.code == 0) {
				$('#ct_' + cid).fadeOut(1000);
				$('#ct_' + cid + '_review_c').html(parseInt($('#ct_' + cid + '_review_c').html()) - 1);
				HideMiniPop();
			}
			else {
				AddMiniHint('屏蔽失败，请重试');
			}
		}
	});
}

function Comment(argus) {
	var did = argus.did;
	log(did);
	var cmt = $('#comment').val();
	$.ajax({
		url: '/Submit/submit.ashx?page=voc&action=008&did=' + did + '&rnd=' + Math.random(),
		type: 'POST',
		timeout: 5000,
		dataType: "json",
		data: { 'cmt': cmt },
		error: function () {
			AddMiniHint('操作失败，请重试');
		},
		success: function (json) {
			if (json.code == 0) {
				var obj = json.obj;
				var csb = new StringBuffer();
				var cid = obj.cid;
				var uid = obj.uid;
				var name = obj.name;

				AddMiniHint('操作成功！');

				$('#comment').val('')

				csb.Append('<div id="ct_' + cid + '" class="item clearfix"><div class="relative"><a class="avatar" target="_blank" href="/user.aspx?uid=' + uid + '"><img src="' + obj.img + '" alt=""></a>');
				csb.Append('<div class="info"><a href="/user.aspx?uid=' + uid + '" target="_blank">' + obj.name + '</a>&nbsp;&nbsp;&nbsp;&nbsp;<span>' + obj.content + '</span></div>');
				csb.Append('<div class="replay-action" data-id="' + cid + '">');

				if (userid == obj.uid) {
					csb.Append('<a id="ct_' + cid + '_o" class="action">删除</a>&nbsp;');
				}
				else {
					csb.Append('<a id="ct_' + cid + '_o" class="action">屏蔽</a>&nbsp;');
				}

				csb.Append('<a class="action">回复</a></div></div>');
				csb.Append('<div class="review hide" id="ct_' + cid + '_review"></div></div>');

				if ($('#ch_' + did).html().trim() == '') {
					$('#ch_' + did).append(csb.toString());
				}
				else {
					$(csb.toString()).insertBefore($('#ch_' + did + '').children().eq(0));
				}

				$('#rd_' + did).html(parseInt($('#rd_' + did).html()) + 1);

				$.each($('#ct_' + cid + ' .action'), function () {
					var txt = $(this).text();
					if (txt == '删除') {
						$(this).bind("click", { fun: DeleteComment, title: "确认删除此评论？", argus: { cid: cid, did: did} }, ShowMiniPop);
					}
					else if (txt == '屏蔽') {
						$(this).bind("click", { fun: ScreenComment, title: "确认屏蔽此评论？", argus: { cid: cid, did: did} }, ShowMiniPop);
					}
					else if (txt == '回复') {
						$(this).bind("click", { did: did, uid: uid, name: name }, ReviewClick);
					}
				});
				HideMiniPop(1000);
			}
			else {
				AddMiniHint('操作失败，请重试');
			}
		}
	});
}

function ReviewClick(e) {
	var data = e.data;
	if (data.uid == userid) {

	}
	$('#comment').val('回复@' + data.name + ' :');
	$('#comment').focus();
}

$(document).ready(function () {

	//首先将#back-to-top隐藏

	$("#back-to-top").hide();

	//当滚动条的位置处于距顶部100像素以下时，跳转链接出现，否则消失

	$(function () {
		$(window).scroll(function () {
			if ($(window).scrollTop() > 100) {
				$("#back-to-top").fadeIn(1500);
			}
			else {
				$("#back-to-top").fadeOut(1500);
			}
		});

		//当点击跳转链接后，回到页面顶部位置

		$("#back-to-top").click(function () {
			$('body,html').animate({ scrollTop: 0 }, 1000);
			return false;
		});
	});
});



function Transform(x) {
	x = BlodPh(x);
	x = x.replace(/\[en\](.*?)\[\/en\]/gim, function (a, b) {
		b = stripSpace(b);
		var d = b.split(' '), m = '';

		for (var i = 0; i < d.length; i++) {
			var di = d[i];

			if (di == 'i.e.') {
				di = di;
			}
			else {
				di = di.replace(/^(\,|，|\.|。|、|\?|？|\"|“|”|\'|‘|’)?(.*?)(\,|，|\.|。|、|\?|？|\"|“|”|\'|‘|’)?$/gim, function (a, b, c, d) {
					if (typeof b == 'undefined') b = '';
					if (typeof d == 'undefined') d = '';
					return b + ' <a title="" target="_blank" href="/Dict.aspx?w=' + c + '">' + c + '</a> ' + d;

				});
			}

			m += di;
		}
		return '<span style="font-size:20px;">' + m + '</span>';
	});
	x = x.replace(/\[cn\](.*?)\[\/cn\]/gim, "<span>$1</span>");
	$('#preview').html(x);
}





